//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package gitcode_triger

import (
	"encoding/json"
	"fmt"

	"atomgit.com/openlinksaas-org/api-server/triger_impl/core"
)

type TagHookInfo struct {
	Before   string      `json:"before"`
	After    string      `json:"after"`
	Ref      string      `json:"ref"`
	UserName string      `json:"user_username"`
	Project  ProjectInfo `json:"project"`
}

func ExtractTagHookAttr(data []byte) ([]string, error) {
	obj := &TagHookInfo{}
	err := json.Unmarshal(data, obj)
	if err != nil {
		return nil, err
	}

	retList := []string{
		fmt.Sprintf("%s=%s", core.GIT_HOOK_TYPE, "tag"),
		fmt.Sprintf("%s=%s", core.GIT_NAME, obj.Project.Name),
		fmt.Sprintf("%s=%s", core.GIT_WEB_URL, obj.Project.WebUrl),
		fmt.Sprintf("%s=%s", core.GIT_HTTP_URL, obj.Project.GitHttpUrl),
		fmt.Sprintf("%s=%s", core.GIT_SSH_URL, obj.Project.GitSshUrl),
		fmt.Sprintf("%s=%s", core.GIT_USERNAME, obj.UserName),

		fmt.Sprintf("%s=%s", core.GIT_TAG_BEFORE, obj.Before),
		fmt.Sprintf("%s=%s", core.GIT_TAG_AFTER, obj.After),
		fmt.Sprintf("%s=%s", core.GIT_TAG_REF, obj.Ref),
	}
	return retList, nil
}
